home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / netprog.zip / NETPROG.TAR / reliable / rudpcli.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  1KB  |  64 lines

  1. /*
  2.  * Example of a more reliable echo client using UDP protocol.
  3.  */
  4.  
  5. #include    "rudp.h"
  6.  
  7. main(argc, argv)
  8. int    argc;
  9. char    *argv[];
  10. {
  11.     int    sockfd;
  12.  
  13.     pname = argv[0];
  14.  
  15.     if ( (sockfd = udp_open(HOST, MYECHO_SERVICE, 0, 0)) < 0)
  16.         err_sys("udp_open error");
  17.  
  18.     doit(stdin, sockfd);        /* do it all */
  19.  
  20.     exit(0);
  21. }
  22.  
  23. /*
  24.  * Read the contents of the FILE *fp, write each line to the
  25.  * socket (to the server process), then read a line back from
  26.  * the socket and print it on the standard output.
  27.  */
  28.  
  29. doit(fp, sockfd)
  30. register FILE        *fp;
  31. register int        sockfd;
  32. {
  33.     int    n, sendlen;
  34.     long    seqsend;
  35.     char    sendline[MAXLINE], recvline[MAXLINE];
  36.  
  37.     seqsend = 0;        /* initialize sequence number to send */
  38.     while (fgets(sendline + sizeof(long), MAXLINE, fp) != NULL) {
  39.         seqsend++;        /* increment sequence number */
  40.         bcopy((char *) &seqsend, sendline, sizeof(long));
  41.         sendlen = strlen(sendline + sizeof(long)) + sizeof(long);
  42. rexmit:
  43.         if ( (n = dgsendrecv(sockfd, sendline, sendlen, recvline,
  44.                                     MAXLINE,
  45.                 (struct sockaddr *) 0, 0)) < 0) {
  46.             if (errno == EINTR)
  47.                 err_sys("client: no response from server");
  48.             else
  49.                 err_dump("client: dgsendrecv error");
  50.         }
  51.  
  52.         if (bcmp((char *) &seqsend, recvline, sizeof(long)) != 0) {
  53.             err_ret("incorrect sequence# received");
  54.             goto rexmit;
  55.         }
  56.  
  57.         recvline[n] = 0;        /* null terminate */
  58.         fputs(recvline + sizeof(long), stdout);
  59.     }
  60.  
  61.     if (ferror(fp))
  62.         err_dump("client: error reading file");
  63. }
  64.